package com.caterina.activities;

import java.awt.BorderLayout;
import java.awt.Container;
import java.util.Collection;

import person.Employee;

import com.caterina.activityManager.CActivity;
import com.caterina.components.CLabel;
import com.caterina.components.CLoading;
import com.caterina.components.CLogo;
import com.caterina.components.CPanel;
import com.caterina.resources.CWords;

import contract.EmployeeContract;
import controllers.ManagerTask.TaskType;
public class StartingActivity extends CActivity{
	public StartingActivity(Container parent) {
		super("STARTING_CATERINA",parent);
		this.setCBarVisible(false);
		this.setCBarQueryesEnabled(false);
	}
	@Override
	public void onData(TaskType task, Object data) {
		switch(task){
			case EMPLOYEE_CONTRACT_GET_ALL:{this.checkHRR(data);break;}
			case SYSTEM_INSERT_SYS_USER:{this.getActivityManager().switchActivity(new InstallationActivity(this.getParentContainer()));break;}
			case USER_LOGIN:{		loadMessage.setText(CWords.getWord("SYS_GOING_TO_INSTALLATION"));this.putTask(TaskType.USER_LOGOUT);break;}
			case USER_LOGOUT:{this.getActivityManager().switchActivity(new InstallationActivity(this.getParentContainer()));break;}
			default:break;
		}
	}
	@SuppressWarnings("unchecked")
	private void checkHRR(Object data){
		loadMessage.setText(CWords.getWord("SYS_CHECK_USERS"));
		Collection<EmployeeContract> dat=(Collection<EmployeeContract>) data;
		for(EmployeeContract emp:dat){
			if(emp.getRole().equals(Employee.Role.HUMAN_RESOURCES_RESPONSABLE)){
				this.putTask(TaskType.USER_LOGIN, "SYS-ADMIN","SYSADM");
				return;
			}
		}
		this.putTask(TaskType.SYSTEM_INSERT_SYS_USER);
	}
	@Override
	public void onError(TaskType task, Exception e) {
		switch (task){
			case USER_LOGIN:{loadMessage.setText(CWords.getWord("SYS_GOING_TO_LOGIN"));this.getActivityManager().switchActivity(new LoginActivity(this.getParentContainer()));break;}
			default: break;
		}
	}
	@Override
	public void onStart() {
		this.stopLoading();
		startingPanel=new CPanel(new BorderLayout());
		startingPanel.add(loading,BorderLayout.CENTER);
		startingPanel.add(getLogoPanel(),BorderLayout.NORTH);
		startingPanel.add(getLoadMsgPanel(),BorderLayout.SOUTH);
		this.add(startingPanel);
		this.putTask(TaskType.EMPLOYEE_CONTRACT_GET_ALL, "");
	}
	private CPanel getLogoPanel(){
		CPanel lPanel= new CPanel();
		lPanel.add(new CLogo(400,110));
		return lPanel;
	}
	private CPanel getLoadMsgPanel(){
		CPanel lPanel= new CPanel();
		lPanel.add(loadMessage);
		return lPanel;
	}
	private CLabel loadMessage=new CLabel(CWords.getWord("SYS_LOADING_CATERINA"));
	private CLoading loading=new CLoading(0);
	private CPanel startingPanel;
	private static final long serialVersionUID = 1L;
}
